
package com.hisense.settings;

import java.util.ArrayList;
import java.util.List;
import com.android.settings.R;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.hisense.settings.base.TVBaseDialog;
import com.hisense.settings.util.HisUtils;
/*import com.marvell.willowtv.framework.features.ParentalControl;
import com.marvell.willowtv.framework.features.TvFramework;
import com.marvell.willowtv.framework.model.atsc.USDTVDownloadableRating;
import com.marvell.willowtv.framework.util.atsc.PCEnumClass.RegionId;*/

public class DynamicDimensionActivity extends TVBaseDialog {

    private static final String TAG = "DynamicDimensionActivity";

    protected ListView ratingList;
    private RatingAdapter mAdapter = null;
    private List<String> rating_names = new ArrayList<String>();
    //private final ParentalControl mPCSettings = TvFramework.getInstance()
    //        .getParentalControlInstance();
    public static final String CURRENT_POSITION = "current_position";
    //private RegionId mRegion = RegionId.REG_DRRT;
    //private USDTVDownloadableRating mDrrtRating = null;
    private int mDimIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mDimIndex = getIntent().getIntExtra(CURRENT_POSITION, 0);
        /*int numLevel = mPCSettings.getNumDinemtionLevel(mRegion, mDimIndex);
        // find that the returned index 0 is a dummy level, so begin from level
        // 1
        for (int i = 1; i < numLevel; i++) {
            rating_names.add(mPCSettings.getRatingText(mRegion, mDimIndex, i).getRatingVal());
        }
        mDrrtRating = mPCSettings.getDRRTRating(mRegion.ordinal());*/
        //dumpDrrtRating();

        setContentView(R.layout.dynamic_ratings);
        ratingList = (ListView) this.findViewById(R.id.rating_list);
        mAdapter = new RatingAdapter(this);
        ratingList.setAdapter(mAdapter);

        setTimeOut(HisUtils.ONE_MINUTE * 2);
    }

    class RatingAdapter extends BaseAdapter {

        private Context context;
        private LayoutInflater inflater;

        public RatingAdapter(Context context) {
            this.context = context;
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return rating_names.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

            if (convertView == null) {
                holder = new ViewHolder();
                convertView = inflater.inflate(R.layout.dynamic_ratings_item, null);
                holder.tv = (TextView) convertView.findViewById(R.id.title);
                holder.ib = (ImageButton) convertView.findViewById(R.id.image_name);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            boolean lock = getLevel(mDimIndex, position);
            Log.d(TAG, "getView Dimension=" + mDimIndex + " position=" + position + " lock=" + lock);
            updateImageButton(holder.ib, lock);
            holder.tv.setText(rating_names.get(position));
            return convertView;
        }

        class ViewHolder {

            TextView tv;
            ImageButton ib;

        }
    }

    /**
     * Called when a key was released .
     * 
     * @param keyCode
     *            integer for the corresponding key
     * @param event
     *            KeyEvent occured
     * @return boolean true if key is handled
     */
    @Override
    public boolean onKeyUp(final int keyCode, final KeyEvent event) {
        Log.d(TAG, "onKeyUp() " + keyCode);
        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) {

            int position = ratingList.getSelectedItemPosition();
            if (position >= 0 && position < mAdapter.getCount()) {
                updateRating(position);
                mAdapter.notifyDataSetChanged();
            }
            return true;

        } else {
            return super.onKeyUp(keyCode, event);
        }
    }

    public void updateImageButton(ImageButton imageButton, boolean lock) {
        Log.v(TAG, "updateImageButton " + lock);

        if (lock) {
            imageButton.setImageResource(R.drawable.lock_icon);
            imageButton.setBackgroundResource(R.drawable.image_lock_style);
            imageButton.setTag(true);
        } else {
            imageButton.setImageResource(R.drawable.unlock_icon);
            imageButton.setBackgroundResource(R.drawable.image_unlock_style);
            imageButton.setTag(false);
        }
    }

    private void updateRating(int index) {
        boolean lock = !getLevel(mDimIndex, index);
        Log.d(TAG, "updateRating Dimension=" + mDimIndex + " position=" + index + " lock=" + lock);
        setLevel(mDimIndex, index, lock);

        //mDrrtRating = mPCSettings.getDRRTRating(mRegion.ordinal());
        //dumpDrrtRating();
    }

    // find that the returned index 0 is a dummy level, so begin from level 1
    private void setLevel(int dimension, int level, boolean lock) {
        //mPCSettings.setDRRTRating(mDrrtRating, dimension, level + 1, lock);
    }

    private boolean getLevel(int dimension, int level) {
        //return mDrrtRating.getDimentionLevel()[dimension][level] != 0;
    	return false;
    }

    /*private void dumpDrrtRating() {
        Log.d(TAG, "dumpDrrtRating mDrrtRating");
        for (int i = 0; i < 20; i++) {
            StringBuilder sbBuilder = new StringBuilder();
            for (int j = 0; j < 15; j++)
                sbBuilder.append(mDrrtRating.getDimentionLevel()[i][j]);
            Log.d(TAG, "Row" + i + "=" + sbBuilder.toString());
        }
    }*/
}
